<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>login</title>
    <style>
      .container {
        border: 1px solid;
        width: 400px;
        height: 500px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      }
      input {
        width: 80%;
        height: 30px;
        border: 1px solid;
        border-radius: 20px;
        margin: 20px;
        text-align: center;
        outline: none;
      }
      button {
        width: 150px;
        height: 45px;
        border: 1px solid;
        border-radius: 20px;
        margin-top: 35px;
        text-align: center;
        line-height: 45px;
        background-color: white;
      }
    </style>
  </head>
  <body>
    <form class="container" target="_self" action="/login" method="post">
      <input
        type="text"
        class="name"
        name="username"
        placeholder="请输入账号"
      />
      <input
        type="password"
        class="pwd"
        name="password"
        placeholder="请输入密码"
      />
      <button type="submit">登录</button>
      <a href="/register">注册</a>
    </form>

    <script src="./jquery.js"></script>
    <script>
      let namepatty = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,7}$/;
      let pwdpatty = /^.{6,}$/;
      let namevalue = $('.name');
      let pwdvalue = $('.pwd');
      let subbtn = $('button[type=submit]');
      let nameyz = () => {
        let name = namevalue.val();
        let res = namepatty.test(name);
        if (!res) {
          namevalue.css('border-color', 'red');
          return false;
        } else {
          namevalue.css('border-color', 'black');
          return true;
        }
      };
      let pwdyz = () => {
        let pwd = pwdvalue.val();
        let res = pwdpatty.test(pwd);
        if (!res) {
          pwdvalue.css('border-color', 'red');
          return false;
        } else {
          pwdvalue.css('border-color', 'black');
          return true;
        }
      };
      subbtn.on('click', (e) => {
        let nameres = nameyz();
        if (!nameres) {
          e.preventDefault();
        }
        let pwdres = pwdyz();
        if (!pwdres) {
          e.preventDefault();
        }
      });
      namevalue.on('input', nameyz);
      pwdvalue.on('input', pwdyz);
    </script>
  </body>
</html>
